package com.yonyou.crm.sprc.oppaction.service;

import com.yonyou.crm.base.doc.service.BaseDocDetailBizServiceImpl;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sprc.oppaction.entity.OppactionVO;
import com.yonyou.crm.sprc.oppdimension.entity.OppdimensionVO;
import com.yonyou.crm.sprc.oppdimension.service.OppdimensionBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

@Service
public class OppactionBizServiceImpl extends BaseBizServiceImpl<OppactionVO> {

	@Autowired
	private EnumBizServiceImpl enumService;

	@Autowired
	private OppdimensionBizServiceImpl oppdimensionService;

	@Autowired
	private BaseDocDetailBizServiceImpl baseDocDetailBizService;
	public List<OppactionVO> getOppactionList(Map<String, Object> paraMap) {

		List<OppactionVO> voList = dao.selectVoList(OppactionVO.class, "listOppaction", paraMap);

		trans(voList);

		return voList;
	}

	public Page<OppactionVO> getList(Page<OppactionVO> page, Map<String, Object> paramMap) {
		{
			Page<OppactionVO> oppactionPage = null;
			if (paramMap != null) {
				oppactionPage = super.pageSelect(OppactionVO.class, page, paramMap.keySet().toArray(new String[0]), paramMap.values().toArray(new Object[0]));
			} else {
				oppactionPage = super.pageSelect(OppactionVO.class, page, null, null);
			}

			trans(oppactionPage.getData());

			return oppactionPage;
		}
	}

	@Override
	protected void beforeSaveVO(OppactionVO vo) {

		super.beforeSaveVO(vo);
		vo.setTenantId(new Long(InvocationInfoProxy.getTenantid()));

		vo.setIsPreseted(new Byte("2"));//非预置

	}

	@Override
	public OppactionVO saveVO(OppactionVO vo) {

		vo = super.saveVO(vo);

		trans(Arrays.asList(vo));

		return vo;
	}

	public OppactionVO getOppactionVOById(Long id){
		OppactionVO vo =  super.getVOById(OppactionVO.class,id);
		trans(Arrays.asList(vo));
		return vo;
	}
	public OppactionVO updateVO(OppactionVO vo) {

		vo = super.updateVO(vo);
		return vo;
	}

	public void batchUpdateEnableState(String[] ids, Integer enableState) {

		Map<String, Object> enableParaMap = new HashMap<String, Object>();
		enableParaMap.put("ids", ids);
		enableParaMap.put("enableState", enableState);
		enableParaMap.put("enableUserId", new Long(InvocationInfoProxy.getParameter("id")));
		enableParaMap.put("enableTime", new Date());

		dao.updateSql(OppactionVO.class, "batchUpdateEnableState", enableParaMap);

	}

	private void trans(List<OppactionVO> voList) {

		if (voList != null && voList.size() > 0) {

			List<FieldRelation<OppactionVO>> relations = new ArrayList<FieldRelation<OppactionVO>>();
//			relations.add(new RefFieldRelation<OppactionVO>(OppactionVO.class, "dimension", new String[]{"dimensionName"}, baseDocDetailBizService, "selectFieldsByIds"));
			relations.add(new EnumFieldRelation<OppactionVO>(OppactionVO.class, "isPreseted", "isPresetedName", enumService, "whether"));
			relations.add(new EnumFieldRelation<OppactionVO>(OppactionVO.class, "enableState", "enableStateName", enumService, "enable_state"));
			relations.add(new RefFieldRelation<OppactionVO>(OppactionVO.class, "dimension", new String[]{"dimensionName"}, oppdimensionService, "selectFieldsByIds"));

			new FieldRelationHandler<OppactionVO>().handler(voList, relations);
		}
	}


	public List<OppactionVO> getByDimension(List ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		List<OppactionVO> voList = dao.selectVoList(OppactionVO.class, "getByDimension", map);
		return  voList;
	}
}
